/**
 * Created with IntelliJ IDEA.
 * User: Lorenzo
 * Date: 15-1-3
 * Time: ����4:50
 * To change this template use File | Settings | File Templates.
 */
var pageSize = 10;
var Global = {
    pageSize : 1,
    pageNum : 1,
    tabManager : "",
    status:"",
    currentFuncName:"",

    init : function() {
    }
}
$('.dropdown').mouseover(function () {
    $('.dropdown-menu').show();
});
$('.dropdown').mouseout(function () {
    $('.dropdown-menu').hide();
})
function initTree(data) {
    var resource = Handlebars.getTemplate("leftTree", "/template/");
    $("#tree").html(Handlebars.complieTemplate(resource, data));
}
function getUserInfo() {
    $("#unameA").text(getCookie("userName"));
//    $.ajax({
//        url: "/rest/status/000001",
//        type: "get",
//        dataType: "json",
//        contentType: "application/json; charset=utf-8",
//        success: function (data) {
//            if (data["status"] === "1")
//                $("#unameA").text(data["name"]);
//        },
//        error: function (data) {
//            alert("error");
//        }
//    });
}

function getMenuTree(pid) {
    var menuTree;
    $.ajax({
        url: "/cm/rbac/cmUser/menu",
        type: "GET",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        async: false,
        success: function (data) {
            menuTree = parseMenuTree(data, pid);
        },
        error: function() {
            data=[{id:"1111",pid:"0000",text:"新增采购组",url:"html/purchasegroup/purchase_group_add.html"},
                {id:"1112",pid:"0000",text:"采购组明细",url:"html/purchasegroup/purchase_group_detail.html"},
                {id:"1113",pid:"0000",text:"修改采购组",url:"html/purchasegroup/purchase_group_update.html"},
                {id:"1114",pid:"0000",text:"查询采购组",url:"html/purchasegroup/purchase_group_query.html"}];
            menuTree = parseMenuTree(data, pid);
        }
    })
    return menuTree;
}

/**
 * format the return data to the example: [{id:"org_man",name:"��֯�ṹ",authority:"1",isDetail:"0",children:[
 * {id:"test1",name:"test1",authority:"1",isDetail:"1"},{id:"test2",name:"test2",authority:"1",isDetail:"1"}]}]
 * this parten should be finished at back-end.

 * @param data
 */
function parseMenuTree(data, pid) {
    var formatData = "["
    if (data.length == 0) {
        formatData = formatData + "}]";
        return formatData;
    }
    for (var i = 0; i < data.length; i++) {
        if (data[i]["pid"] == pid) {
            formatData = formatData + '{id:' + '"' + data[i]['id'] + '",name:"' + data[i]['text'] + '",authority:"1",isDetail:"';
            if (data[i]["url"] != undefined && data[i]["url"] != "") {
                formatData = formatData + '1",children:[], href:"' + data[i]["url"] + '"},';
            } else {
                formatData = formatData + '0",children:[';
                for (var j = 0; j < data.length; j++) {
                    if (data[j]["pid"] == data[i]["id"]) {
                        formatData = formatData + '{id:' + '"' + data[j]['id'] + '",name:"' + data[j]['text'] + '",authority:"1",isDetail:"';
                        if (data[j]["url"] != undefined && data[j]["url"] != "") {
                            formatData = formatData + '1",children:[], href:"' + data[j]["url"] + '"},';
                        } else {
                            formatData = formatData + '0",children:[';
                            for (var k = 0; k < data.length; k++) {
                                if (data[k]["pid"] == data[j]["id"]) {
                                    formatData = formatData + '{id:' + '"' + data[k]['id'] + '",name:"' + data[k]['text'] + '",authority:"1",isDetail:"';
                                    if (data[k]["url"] != undefined && data[k]["url"] != "") {
                                        formatData = formatData + '1",children:[], href:"' + data[k]["url"] + '"},';
                                    } else {
                                        formatData = formatData + '0",children:[]},';
                                    }
                                }
                            }
                            formatData = formatData + '], href:"' + data[j]["url"] + '"},';
                        }
                    }
                }
                formatData = formatData + '], href:"' + data[i]["url"] + '"},';
            }
        }
    }
    formatData = formatData + ']';
    return formatData;
}

function invokeFrame(id, name, url) {
    $("#pageContent").ligerTab({height:600});
    Global.tabManager = $("#pageContent").ligerGetTabManager();
    if("tabid-1111"==id) {
        Global.pageStatus = "save";
        Global.dataId = "";
        id = "purchaseGroup_save";
    }else if("tabid-1112"==id) {
        Global.pageStatus = "detail";
        Global.dataId = "CGZ0011";
        id="purchaseGroup_detail";
    }else if("tabid-1113"==id) {
        Global.pageStatus = "update";
        Global.dataId = "CGZ0011";
        id="purchaseGroup_update";
    }else if("tabid-1114"==id) {
        Global.pageStatus = "query";
        Global.dataId = "";
        id="purchaseGroup_query";
    }
    Global.tabManager.addTabItem({tabid:id, text:name, url:url});
}

$(document).ready(function () {
    validateCookie();
    getUserInfo();
    var dd = eval('('+getMenuTree("0000")+')');
    initTree(dd);

  //������֯��
  $('#sys_manage').click(function () {
      var data = eval('('+getMenuTree("0100")+')');
      initTree(data);
  });
  $('#person_manage').click(function () {
      var data = eval('(' + getMenuTree("0010") + ')');
      initTree(data);
  });
  $('#base_data').click(function () {
      var data = eval('(' + getMenuTree("0200") + ')');
      initTree(data);
  });
  $('#store_manage').click(function () {
      var data = eval('(' + getMenuTree("0300") + ')');
      initTree(data);
  });
  $('#purchase_manage').click(function () {
      var data = eval('(' + getMenuTree("0400") + ')');
      initTree(data);
  });
  $('#sell_manage').click(function () {
      var data = eval('(' + getMenuTree("0500") + ')');
      initTree(data);
  });
  $('#product_manage').click(function () {
      var data = eval('(' + getMenuTree("0600") + ')');
      initTree(data);
  });
  $('#quality_manage').click(function () {
      var data = eval('(' + getMenuTree("0700") + ')');
      initTree(data);
  });
  $('#plan_manage').click(function () {
      var data = eval('(' + getMenuTree("0800") + ')');
      initTree(data);
  });
  $('#flow_manage').click(function () {
      var data = eval('(' + getMenuTree("0900") + ')');
      initTree(data);
  });
  Global.init();
});

function logout(){
    deleteCookie("userName");
    window.location.href="/login.html";
}
